<template>
    <view class="addinvestigationlist_body" id="app">
		<!-- 选择会员 -->
		<div class="add-hd">
			<div class="add-hd-l">
				<p>填写时间：
					<picker mode="date" :start="start" :end="end"  @change="bindDateChange">
						<view class="date">{{start_date}}</view>
					</picker> 
				</p>
			</div>
		</div>
        <uni-collapse :accordion="accordion">
            <!-- <uni-collapse-item v-for="(item,index) in QuestionnaireStencil" :key="index" :title="index>3 ? item.name+ '(必选项)': item.name" :name="index" showAnimation :titleStyle="{'background':'#36AB7C','color':'#fff'}"> -->
            <uni-collapse-item v-for="(item,index) in QuestionnaireStencil" :key="index" :title="item.name" :name="index" showAnimation :titleStyle="{'background':'#36AB7C','color':'#fff'}">
                <div v-if="index == 0">
                    <div v-for="(a,aindex) in item.data" class="item">
                        <p class="que"><i>{{aindex+1}}</i>{{a.name}}：</p>
						<checkbox-group>
							<label  v-for="(b,bi) in a.data" :key="bi">
								<checkbox :value="b.questionNo" color="#007aff" :checked="data[b.questionNo]" :disabled="questionStatus==2" @click="checkboxChange(b)">{{b.questionTitle}}</checkbox>
							</label>
						</checkbox-group>
                    </div>
                </div>
                <div v-else-if="index == 1 || index ==2">
					<div class="item">
						<checkbox-group>
							<label v-for="(a,aindex) in item.data" :key="aindex">
								<checkbox color="#007aff" :value="a.questionNo" :checked="data[a.questionNo]" :disabled="questionStatus==2" @click="checkboxChange(a)" >{{a.questionTitle}}</checkbox>
							</label>
						</checkbox-group>
					</div>
                </div>
                <div v-else>
					<div v-for="(a,aindex) in item.data">
						<div v-if="a.name" :class="a.className">
							<div class="item">
								<p class="que"><i>{{aindex+1}}</i>{{a.name}}：</p>
								<checkbox-group>
									<label v-for="(b,bindex) in a.data" :key="bindex">
										<checkbox color="#007aff" :value="b.questionNo" :checked="data[b.questionNo]" :disabled="questionStatus==2" @click="checkboxChange(b)">{{b.questionTitle}}</checkbox>
									</label>
								</checkbox-group>
							</div>
						</div>
						<div :class="a.className" v-else>
							<div class="item" v-show="a.show">
								<p class="que" v-show="a.show"><i>{{aindex+1}}</i>{{a.questionTitle}}：<span v-if="a.questionTitle != '常吃早餐' && a.questionTitle != '常吃夜宵'"></span></p>
								<radio-group @change="radioChange">
									<label v-for="(b, bi) in a.data" :key="bi" @click="radioCheck(a,bi,b)">
										<view>
											<radio :value="JSON.stringify({'questionNo':b.questionNo,'answerOption':b.answerOption})" :disabled="questionStatus==2" :checked="data[b.questionNo] == b.answerOption"/>
										</view>
										<view>{{b.answerTxt}}</view>
									</label>
								</radio-group>
							</div>
						</div>
					</div>
                </div>
            </uni-collapse-item>
        </uni-collapse>
		
		<!-- 会员弹框 -->
		<member :dialog.sync="dialog"  @hidedialog="hidedialog" @chooseUser="chooseUser"></member>
		
		<u-modal v-model="showModal" :content="showContent" :show-cancel-button="true" @confirm="sureconfirm"></u-modal>
    </view>
</template>
<script>
import uniCollapse from '@/components/uni-collapse/uni-collapse.vue'
import uniCollapseItem from '@/components/uni-collapse-item/uni-collapse-item.vue'
import member from '@/components/selectMember/selectMember.vue'
import {getUrlParam,filterData2} from '@/static/js/filter.js'
export default{
	components: {uniCollapse,uniCollapseItem,member},
	props:['userId'],
	data(){
		const currentDate = this.getDate({
			format: true
		})
		return{
			list:[],
			data:{},
			activeNames: [],
			minDate: new Date(2000, 0, 1),
			maxDate: new Date(),
			QuestionnaireStencil:[],
			show:false,
			questionNumber:'',
			questionTime:'',
			resultData:[] ,//最后要上传的数据
			questionStatus:'',
			reportUrl: '',
			accordion: true,
			dialog:false,
			userName:'',
			start: '',
			end: currentDate,
			start_date:'',
			showModal:false,
			showContent:'',
			isChoose:true,
			entrance:''
		}
	},
	mounted() {
		this.$api.getQuestionnaireStencil({type:1}).then(res=>{
			this.filterArr(res.data.result)
		})
	},
	methods: {
		userChange(userid,questionNumber,sex){
			const _this = this;
			if(questionNumber){
				_this.$api.Questionnairefind(userid,questionNumber,{type:1}).then((res)=>{
					_this.resultData = res.data.result.questionnaireAnswerVoList
					res.data.result && res.data.result.questionnaireAnswerVoList && res.data.result.questionnaireAnswerVoList.map((e)=>{
						_this.$set(_this.data,[e.questionNo],e.answerOption)
						_this.start_date = res.data.result.questionTime
						_this.editid = res.data.result.id
					})
				})
			}
		},
		// 获取当前时间
		getDate(type) {
		   const date = new Date();
		   let year = date.getFullYear();
		   let month = date.getMonth() + 1;
		   let day = date.getDate();
		  
		   if (type === 'start') {
			year = year - 60;
		   } else if (type === 'end') {
			year = year + 2;
		   }
		   month = month > 9 ? month : '0' + month;;
		   day = day > 9 ? day : '0' + day;
		   return `${year}-${month}-${day}`;
		},
		hidedialog() {
			this.dialog = false
		},
		chooseUser(e){ //选择会员
			this.userName = e.userName;
			this.userId = e.userId;
		},
		// 日期
		bindDateChange(e) {  //日期选择
			this.start_date = e.target.value;
		},
		radioChange(e){
			const value = JSON.parse(e.detail.value)
			const hasDataIndex = this.resultData.findIndex(a=>{return a.questionNo == value.questionNo})
			if(hasDataIndex > -1){
				this.resultData[hasDataIndex].answerOption = value.answerOption
				delete this.data[value.questionNo]
			}else{
				this.resultData.push({questionNo:value.questionNo,answerOption:value.answerOption})
				this.data[value.questionNo] = 1
			}
		},
		checkboxChange(e){
			const hasIndex = this.resultData.findIndex(a=>{return a.questionNo == e.questionNo})
			if(hasIndex > -1){
				this.resultData.splice(hasIndex,1)
				delete this.data[e.questionNo]
			}else{
				this.resultData.push({questionNo:e.questionNo,answerOption:1})
				this.data[e.questionNo] = 1
			}
		},
		submit(){
			const _this = this
			if(!_this.userId){
				uni.showToast({title:"请选择会员",icon:'none'});
				return;
			}
			if(!_this.start_date){
				uni.showToast({title:"请选择填写日期",icon:'none'});
				return;
			}
			this.showModal = true
			this.showContent = '确认信息无误,签名并发送给用户存入健康档案,发送后该信息将不可修改';
			return
		},
		sureconfirm() {
			const obj = {appQuestionnaireVoList:this.resultData,questionTime:this.start_date,status:2,questionType:1}
			this.$api.uploadQuestionnaire(this.userId,obj).then((res)=>{
				if(res.data.returnCode == 200){
					uni.showToast({title: '签发成功',icon:'none'})
				}else{
					uni.showToast({title: res.data.message,icon:'none'});
				}
			})
		},
		// 问卷整合
		filterArr(e){
			let _this = this;
			let arr = []
			e.map((ev,i)=>{
				let hasIndex = arr.findIndex((av,ai)=>{ return av.questionNo == ev.questionNo})
				if(hasIndex > -1){
					ev.checked = false
					ev.disabled = _this.questionStatus == 2?true:false
					arr[hasIndex].data.push(ev)
				}else{
					ev.checked = false
					ev.disabled = _this.questionStatus == 2?true:false
					arr.push({data:[ev],questionNo:ev.questionNo,questionTitle:ev.questionTitle,show:true})
				}
			})
			// 
			// 家族病史
			var gxy = {name:'高血压病',data:arr.filter((e,i)=>{ return e.questionTitle.indexOf('高血压病') > -1 &&  e.questionTitle.indexOf('本人') == -1})} // 高血压
			var xzyc = {name:'血脂异常',data:arr.filter((e,i)=>{return e.questionTitle.indexOf('血脂异常') > -1 &&  e.questionTitle.indexOf('本人') == -1})} // 血脂异常	
			var gzss = {name:'骨质疏松',data:arr.filter((e,i)=>{return e.questionTitle.indexOf('骨质疏松') > -1 &&  e.questionTitle.indexOf('本人') == -1})} // 骨质疏松	
			var ga = {name:'肝癌',data:arr.filter((e,i)=>{return e.questionTitle.indexOf('肝癌') > -1 &&  e.questionTitle.indexOf('本人') == -1})} // 肝癌	
			var rxa = {name:'乳腺癌',data:arr.filter((e,i)=>{return e.questionTitle.indexOf('乳腺癌') > -1 &&  e.questionTitle.indexOf('本人') == -1})} // 乳腺癌	
			var tnb = {name:'糖尿病',data:arr.filter((e,i)=>{return e.questionTitle.indexOf('糖尿病') > -1 &&  e.questionTitle.indexOf('本人') == -1})} // 糖尿病	
			var fpz = {name:'肥胖症',data:arr.filter((e,i)=>{return e.questionTitle.indexOf('肥胖症') > -1 &&  e.questionTitle.indexOf('本人') == -1})} // 肥胖症	
			var aezhmb = {name:'阿尔茨海默病',data:arr.filter((e,i)=>{return e.questionTitle.indexOf('阿尔茨海默病') > -1 &&  e.questionTitle.indexOf('本人') == -1})} // 阿尔茨海默病	
			var wa = {name:'胃癌',data:arr.filter((e,i)=>{return e.questionTitle.indexOf('胃癌') > -1 &&  e.questionTitle.indexOf('本人') == -1})} // 胃癌	
			var gja = {name:'宫颈癌',data:arr.filter((e,i)=>{return e.questionTitle.indexOf('宫颈癌') > -1 &&  e.questionTitle.indexOf('本人') == -1})} // 宫颈癌	
			var gxb = {name:'冠心病',data:arr.filter((e,i)=>{return e.questionTitle.indexOf('冠心病') > -1 &&  e.questionTitle.indexOf('本人') == -1})} // 冠心病	
			var ncz = {name:'脑卒中',data:arr.filter((e,i)=>{return e.questionTitle.indexOf('脑卒中') > -1 &&  e.questionTitle.indexOf('本人') == -1})} // 脑卒中	
			var fa = {name:'肺癌',data:arr.filter((e,i)=>{return e.questionTitle.indexOf('肺癌') > -1 &&  e.questionTitle.indexOf('本人') == -1})} // 肺癌	
			var qlxa = {name:'前列腺癌',data:arr.filter((e,i)=>{return e.questionTitle.indexOf('前列腺癌') > -1 &&  e.questionTitle.indexOf('本人') == -1})} // 前列腺癌
			gxy.data.map((a)=>{ a.questionTitle = a.questionTitle.replace('高血压病',''); return a})
			xzyc.data.map((a)=>{ a.questionTitle = a.questionTitle.replace('血脂异常',''); return a})
			gzss.data.map((a)=>{ a.questionTitle = a.questionTitle.replace('骨质疏松',''); return a})
			ga.data.map((a)=>{ a.questionTitle = a.questionTitle.replace('肝癌',''); return a})
			rxa.data.map((a)=>{ a.questionTitle = a.questionTitle.replace('乳腺癌',''); return a})
			tnb.data.map((a)=>{ a.questionTitle = a.questionTitle.replace('糖尿病',''); return a})
			fpz.data.map((a)=>{ a.questionTitle = a.questionTitle.replace('肥胖症',''); return a})
			aezhmb.data.map((a)=>{ a.questionTitle = a.questionTitle.replace('阿尔茨海默病',''); return a})
			wa.data.map((a)=>{ a.questionTitle = a.questionTitle.replace('胃癌',''); return a})
			gja.data.map((a)=>{ a.questionTitle = a.questionTitle.replace('宫颈癌',''); return a})
			gxb.data.map((a)=>{ a.questionTitle = a.questionTitle.replace('冠心病',''); return a})
			ncz.data.map((a)=>{ a.questionTitle = a.questionTitle.replace('脑卒中',''); return a})
			fa.data.map((a)=>{ a.questionTitle = a.questionTitle.replace('肺癌',''); return a})
			qlxa.data.map((a)=>{ a.questionTitle = a.questionTitle.replace('前列腺癌',''); return a})
			const jzbs = {name:'家族病史',data:[gxy,xzyc,gzss,ga,rxa,tnb,fpz,aezhmb,wa,gja,gxb,ncz,fa,qlxa]}

			// 患病情况
			const hbqk = {name:'患病情况',data:arr.filter((e,i)=>{ 
				return e.questionTitle.indexOf('本人') > -1  
			})}
			hbqk.data = hbqk.data.map((a,aindex)=>{
				a.questionTitle = a.questionTitle.replace('本人','')
				return a
			}).filter(b=>{
				if(this.sex == '男'){
					return b.questionTitle != '宫颈癌' && b.questionTitle != '乳腺癌'
				}else{
					return b.questionTitle != '前列腺癌'
				}
			})
			// 既往情况
			const jwqk = {name:'既往情况',data:arr.filter((e,i)=>{ 
				return e.questionTitle.indexOf('油烟') > -1  ||
				e.questionTitle.indexOf('粉烟尘') > -1  || 
				e.questionTitle.indexOf('毒物致癌物') > -1  || 
				e.questionTitle.indexOf('高温') > -1  || 
				e.questionTitle.indexOf('低温') > -1  || 
				e.questionTitle.indexOf('辐射') > -1  || 
				e.questionTitle.indexOf('噪音或震动') > -1  
			})}
			// 饮食习惯
			var ysxg = {name:'饮食习惯',data:arr.filter((e,i)=>{ 
				return  e.questionTitle.indexOf('常吃早餐') > -1 ||
				e.questionTitle.indexOf('常吃夜宵') > -1
			})}
			this.ysxg = ysxg;

			var ysxgA1 = arr.filter((a)=>{
				return a.questionTitle.indexOf('喜好清淡') > -1 || 
					   a.questionTitle.indexOf('喜好咸') > -1 || 
					   a.questionTitle.indexOf('喜好甜') > -1 || 
					   a.questionTitle.indexOf('喜好生') > -1 || 
					   a.questionTitle.indexOf('喜好冷') > -1 || 
					   a.questionTitle.indexOf('喜好硬') > -1 || 
					   a.questionTitle.indexOf('喜好热烫') > -1 || 
					   a.questionTitle.indexOf('喜好高油脂') > -1 || 
					   a.questionTitle.indexOf('喜好烟熏') > -1 || 
					   a.questionTitle.indexOf('喜好辛辣') > -1
			})
			var ysxgA1data = [{name:'您的饮食口味',data:ysxgA1.map((a)=>{
				a.questionTitle = a.questionTitle.replace('饮食喜好','')
				return a
			})}]
			var ysxgA2 = arr.filter((a)=>{
				return a.questionTitle.indexOf('过') > -1 || a.questionTitle.indexOf('吃饭时喝水') > -1
			})
			var ysxgA3 = arr.filter((a)=>{
				return a.questionTitle.indexOf('油炸食品') > -1 || 
					   a.questionTitle.indexOf('甜点') > -1 || 
					   a.questionTitle.indexOf('吃零食') > -1 || 
					   a.questionTitle.indexOf('经常吃快餐') > -1 || 
					   a.questionTitle.indexOf('喝粥') > -1
			})
			var ysxgA2data = [{name:'您目前饮食的不良习惯',data:ysxgA2}]
			var ysxgA3data = [{name:'您的饮食偏好',data:ysxgA3,className:'long'}]

			ysxg.data = ysxg.data.concat(ysxgA2data,ysxgA1data,ysxgA3data)
			// console.log(ysxg)
			// 个人膳食摄入
			const grss = {name:'个人膳食摄入',data:arr.filter((e,i)=>{ 
				return e.questionTitle.indexOf('每天食量') > -1 ||
				e.questionTitle.indexOf('水每天饮用量') > -1 
			})}
			this.grss = grss;

			// 运动锻炼
			const yddl = {name:'运动锻炼',data:arr.filter((e,i)=>{ 
				return e.questionTitle.indexOf('工作时间') > -1 ||
				e.questionTitle.indexOf('静坐时间') > -1 ||
				e.questionTitle.indexOf('运动锻炼') > -1 
			})}
			this.yddl = yddl;
			// 吸烟情况
			const xyxg = {name:'吸烟情况',data:arr.filter((e,i)=>{ 
				return e.questionTitle.indexOf('吸烟') > -1 || e.questionTitle.indexOf('平均每天几支') > -1 
			})}
			this.xyxgArr = xyxg
			// 饮酒情况
			const yjxg = {name:'饮酒情况',data:arr.filter((e,i)=>{ 
				return e.questionTitle.indexOf('饮酒') > -1 || e.questionTitle.indexOf('平均每天几两') > -1 
			})}
			this.yjqkArr = yjxg

			// 心理健康
			const xljk = {name:'心理健康',data:arr.filter((e,i)=>{ 
				return e.questionTitle.indexOf('困扰') > -1 || e.questionTitle.indexOf('情绪') > -1 || e.questionTitle.indexOf('压力') > -1 
			})}
			this.xljk = xljk;
			// 睡眠健康
			const smjk = {name:'睡眠健康',data:arr.filter((e,i)=>{ 
				return e.questionTitle.indexOf('睡眠') > -1 
			})}
			this.smjk = smjk;

			// 其他
			const qt = {name:'其他',data:arr.filter((e,i)=>{ 
				return e.questionTitle.indexOf('皮质激素服用') > -1 
			})}
			this.qt = qt;
			this.QuestionnaireStencil.push(jzbs,hbqk,jwqk,ysxg,grss,yddl,xyxg,yjxg,xljk,smjk,qt)
			// console.log(this.QuestionnaireStencil)
		},
		radioCheck(a,bi,b){
			if(this.questionStatus == 2){
				return;
			}
			if(!a.data[bi].checked){
				a.data.map(res=>{res.checked = false})
				a.data[bi].checked = true
				this.data[a.questionNo] = b.answerOption
			}else{
				a.data[bi].checked = false
				this.$set(this.data,[a.questionNo],'')
			}
			
			if(b.questionNo == 'e01' && this.data[a.questionNo] == '1'){
				this.xyxgArr.data.map((res)=>{
					// console.log(res)
					if(res.questionNo != 'e01' && res.questionNo != 'e05'){
						res.show = false
						// this.data[res.questionNo] = ''
					}
				})
			}else if(b.questionNo == 'e01' && this.data[a.questionNo] != '1'){
				this.xyxgArr.data.map((res)=>{
						res.show = true
				})
			}

			if(b.questionNo == 'f01' && this.data[a.questionNo] == '1'){
				this.yjqkArr.data.map((res)=>{
					if(res.questionNo != 'f01'){
						res.show = false
					}
				})
			}else if(b.questionNo == 'f01' && this.data[a.questionNo] != '1'){
				this.yjqkArr.data.map((res)=>{
					res.show = true
				})
			}
		}
	}
}
</script>
<style lang="scss" scoped>
	.add-hd{padding:34upx 24upx;display: flex;align-items: center;width: 702upx;z-index: 100;margin-top: 130upx;}
	.add-hd-l{flex:1;color: #333333;}
	.add-hd-l p{font-size: 30rpx;display: flex;align-items: center;}
	picker{width: 330rpx;height: 46rpx;border: 1rpx solid #E4E4E4;line-height: 46rpx;padding-left: 10rpx;}
	.add-hd-l span{width: 329upx;height: 46upx;border: 1upx solid #E4E4E4;border-radius: 6upx;}
	.add-hd-r button{width: 180upx;height: 70upx;background: #36AB7C;border-radius: 8upx;color: #fff;font-size: 28upx;line-height: 70upx;}
	.que{font-size: 26upx;color: #333;background: #fff;}
	.que i{font-style: normal;display: inline-block;padding:2upx 12upx;background: #3CBE8A;color: #fff;line-height: 1;border-radius:2upx;font-size: 16upx;margin-right: 8upx;}
	.item{
		padding:20upx;
		background: #fff;
		.uni-label-pointer,uni-radio-group{
			display: flex;
			flex-wrap: wrap;
			font-size: 22upx;
			uni-checkbox,.uni-label-pointer{
				width:50%;
				margin-top: 24upx;
			}
		}
		uni-checkbox-group{
			display: flex;
			flex-wrap: wrap;
			font-size: 22upx;
			uni-label{
				width:50%;
				margin-top: 12upx;
				uni-checkbox{
					width: 100% !important;
					
				}
			}
		}
	}
	.uni-collapse {
		// margin-top: 130upx;
		/deep/ .uni-checkbox-input-checked,/deep/ .uni-radio-input-checked{
			background: #3CBE8A !important;
			color: #fff !important;
			&:before{
				font-size: 36upx !important;
			}
		}
		/deep/ uni-checkbox .uni-checkbox-input{
			border-radius: 8upx;
			width: 28rpx;
			height: 28rpx;
		}
		/deep/ uni-radio .uni-radio-input{
			border-radius: 8upx;
			width: 28rpx;
			height: 28rpx;
		}
	}
	
	
	/deep/ .uni-collapse-cell--hide{
		height: 78upx !important;
	}
	/deep/ .uni-collapse-cell__title{
		background: #ECFBF6 !important;
		height: 78upx !important;
		border-bottom: 1rpx solid #fff;
	}
	/deep/ .uni-collapse-cell__title-text{
		padding-left: 30upx;
		span{
			color: #38CB97;
		}
		&::before{
			content: '';
			position: absolute;
			width: 10upx;
			height: 30upx;
			background: #38CB97;
			left: 20upx;
			top: 32%;
			border-radius: 4upx;
		}
	}
	/deep/ .uni-icons{
		color: #999999 !important;
	}
	.selectMember {
		position: fixed;
		top: 88upx;
		left: 0;
		width: 750upx;
		height: 130upx;
		background-color: #38CB97;
		padding: 34upx 24upx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		z-index: 10;
		.member {
			flex: 1;
			margin-right: 45upx;
			height: 62upx;
			background-color: #FFFFFF;
			border-radius: 6upx;
			line-height: 62upx;
			image {
				width: 26upx;
				height: 26upx;
				margin: 0 18upx;
			}
			text {
				color: #38CB97;
				font-size: 30upx;
			}
		}
		.save {
			width: 138upx;
			height: 62upx;
			line-height: 62upx;
			text-align: center;
			border-radius: 8upx;
			background: #D0FFEE;
			color: #333333;
			margin-right: 19upx;
			font-size: 28upx;
		}
		.toSend {
			background-color: #16AE78;
			width: 138upx;
			height: 62upx;
			line-height: 62upx;
			text-align: center;
			border-radius: 8upx;
			color: #FFFFFF;
			font-size: 28upx;
		}
	}
</style>